package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;


    //查询所有部门
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll() {
        //将数据转换成json格式
        List<Department> departments = departmentService.selectAll();

        return departments;
    }

    @RequestMapping("query")
    @ResponseBody
    public Object list(@ModelAttribute("qo") QueryObject qo, Model model) {
        //将数据转换成json格式
        PageResult result = departmentService.queryForList(qo);

        return result;
    }

    @RequestMapping("view")

    public String list1(Model model, Long id) {

        return "/department/list";
    }


    @RequestMapping("input")



    public String input(Model model, Long id) {
        if (id != null) {
            //update
            Department entity = departmentService.selectByPrimaryKey(id);
            model.addAttribute("entity", entity);
        }

        return "/department/input";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"department:saveOrUpdate","部门保存与更新"},logical = Logical.OR )
    public Object saveOrUpdate( Department entity) {
       departmentService.saveOrUpdate(entity);

        return new JSONResult();
    }

    @ResponseBody
    @RequestMapping("changeState")
    @RequiresPermissions(value = {"department:changeState","部门状态"},logical = Logical.OR )
    public Object changeState(Long id){
        departmentService.changeState(id);
        JSONResult json = new JSONResult();
        return json;
    }

    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"department:deleteByPrimaryKey","部门删除"},logical = Logical.OR )
    public Object delete( Long id) {
       departmentService.deleteByPrimaryKey(id);

        return new JSONResult();
    }


}
